package com.frkj.yuan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.api.bean.BaseStringMsgBean;
import com.frkj.yuan.api.bean.JiuzhenPersonalListBean;
import com.frkj.yuan.api.category.JiuzhenPersonListCatefory;
import com.frkj.yuan.api.presenter.JiuzhenPersonListPresenter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.dialog.LoginOutDialog;
import com.frkj.yuan.dialog.OnQuiteDialogClickListener;
import com.frkj.yuan.dialog.RoundCornerDialog;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.frkj.yuan.view.RecyclerViewLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：菲菲 on 2022/2/16 13:57
 * company：丰蓉信息科技有限公司
 */
public class JiuZhengPesonalActivity extends BaseActivity implements JiuzhenPersonListCatefory.View, OnRefreshListener {
    @BindView(R.id.base_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.relat_empty)
    RelativeLayout mEmptyLayout;
    @BindView(R.id.order_msg)
    TextView mOrderMsg;
    private String mToken;
    private JiuzhenPersonListPresenter mPresenter;
    private long mUserId;
    private int pageNumber = 1;
    private BaseRecyclerRefreshAdapter<JiuzhenPersonalListBean.JiuzhenPersonalListBeanData.JiuzhenPersonalListBeanDataList> mAdapter;
    private List<JiuzhenPersonalListBean.JiuzhenPersonalListBeanData.JiuzhenPersonalListBeanDataList> mlist = new ArrayList<>();
    private JSONObject result;
    private boolean isLoading;
    private String mPerson;
    private LoginOutDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_jiuzheng_view;
    }

    @OnClick({R.id.add_personal, R.id.base_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.add_personal:
                openActivityWitchAnimation(AddJiuZhengPersonalActivity.class);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void initLayoutView() {
        //设置状态栏为黑色
        FullScreenUtils.getInstance().fullScreen(JiuZhengPesonalActivity.this, true);
        mPerson = getIntent().getStringExtra("person");
        mToken = (String) SPUtils.get(JiuZhengPesonalActivity.this, ConstantStaticUrl.mToken, "");
        SharedPreferences sharedPreferences = getSharedPreferences("First", MODE_PRIVATE);
        RecyclerViewLayoutManager.setLinearVerticalManager(JiuZhengPesonalActivity.this, mRecyclerView);
        mUserId = sharedPreferences.getLong(ConstantStaticUrl.userId, 0);
        mTitleMsg.setText("就诊人员");
        mPresenter = new JiuzhenPersonListPresenter(this);
        mAdapter = new BaseRecyclerRefreshAdapter<JiuzhenPersonalListBean.JiuzhenPersonalListBeanData.JiuzhenPersonalListBeanDataList>(JiuZhengPesonalActivity.this, mlist, R.layout.jiuzheng_item_view) {
            @Override
            public void convert(BaseRecyclerHolder holder, JiuzhenPersonalListBean.JiuzhenPersonalListBeanData.JiuzhenPersonalListBeanDataList item, int position, boolean isScrolling) throws IOException {
                holder.setText(R.id.jz_name, item.getName());
                holder.setText(R.id.jz_msg, item.getRelationStr());
                if (item.getGender() == 0) {
                    holder.setText(R.id.jz_gender, "女");
                } else {
                    holder.setText(R.id.jz_gender, "男");
                }
                holder.setText(R.id.jz_mobile, item.getContact());
                holder.setText(R.id.jz_idcard, item.getCardNum());
                holder.getView(R.id.jk_car).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        openActivityWitchAnimation(HealthCardActivity.class);
                    }
                });
                holder.getView(R.id.delte_item).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RoundCornerDialog dialog = new RoundCornerDialog(JiuZhengPesonalActivity.this, "确定要删除吗？", "确定", "取消", new OnQuiteDialogClickListener() {
                            @Override
                            public void onConfirm(Dialog dialog) {
                                mPresenter.getDelete(mToken, item.getId(), item.getUserId());
                                mlist.remove(position);
                                mAdapter.notifyItemRemoved(position);
                                mAdapter.notifyItemChanged(0, mlist.size());
                                mRefreshLayout.autoRefresh();
                                dialog.dismiss();
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                    }
                });
            }
        };
        mAdapter.setDatas(mlist);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                if (mPerson != null && !mPerson.equals("")) {
//                    Bundle bundle = new Bundle();
//                    bundle.putLong("mJiuPersonId", mlist.get(position).getId());
//                    bundle.putString("name", mlist.get(position).getName());
//                    bundle.putString("sfzhm", mlist.get(position).getCardNum());
//                    bundle.putString("sjhm", mlist.get(position).getContact());
//                    bundle.putString("jzgx", mlist.get(position).getRelationStr());
//                    openActivityWitchAnimation(JiuzhenMsgActivity.class, bundle);
//                    finish();
//                    overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);

                    Intent bundle = new Intent();
                    //把返回数据存入Intent
                    bundle.putExtra("mJiuPersonId", mlist.get(position).getId());
                    bundle.putExtra("name", mlist.get(position).getName());
                    bundle.putExtra("sfzhm", mlist.get(position).getCardNum());
                    bundle.putExtra("sjhm", mlist.get(position).getContact());
                    bundle.putExtra("jzgx", mlist.get(position).getRelationStr());
                    //设置返回数据
                    JiuZhengPesonalActivity.this.setResult(200, bundle);//RESULT_OK为自定义常量
                    //关闭Activity
                    JiuZhengPesonalActivity.this.finish();
                }
            }
        });
        SetPersenter(mPresenter);
        result = new JSONObject();
        try {
            result.put(ConstantStaticUrl.userId, mUserId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mPresenter.getAddjiuzhenPerson(mToken, pageNumber, 10, result.toString());
        mRefreshLayout.setOnRefreshListener(this);

    }

    @Override
    public void getAddjiuzhenPerson(JiuzhenPersonalListBean jiuzhen) {
        if (jiuzhen.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (jiuzhen.getData() != null && jiuzhen.getData().getList().size() > 0) {
                if (pageNumber == 1) {
                    if (isLoading) {
                        isLoading = false;
                    } else {
                        mlist.clear();
                    }
                    mlist.addAll(jiuzhen.getData().getList());
                    mAdapter.setDatas(jiuzhen.getData().getList());
                } else {
                    mlist.addAll(mlist.size(), jiuzhen.getData().getList());
                    mAdapter.add(jiuzhen.getData().getList());
                }
            } else {
                if (isLoading) {
                    isLoading = false;
                }
            }
        } else if (jiuzhen.getCode() == 401) {
            if (dialog == null){
                dialog = new LoginOutDialog(JiuZhengPesonalActivity.this);
            }
            if(dialog != null  && !dialog.isShowing()){
                dialog.show();
            }
            dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(UserLoginActivity.class);
                    SPUtils.clear(JiuZhengPesonalActivity.this);
                    JiuZhengPesonalActivity.this.finish();
                    dialog.dismiss();
                }
            });
        }
    }

    @Override
    public void DeleteMsg(BaseStringMsgBean msgBean) {
        if (msgBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            ToastUtil.showToastSUCCESS(JiuZhengPesonalActivity.this, msgBean.getMsg());
        } else {
            ToastUtil.showToastINFO(JiuZhengPesonalActivity.this, msgBean.getMsg());
        }
    }
    @Override
    public void SetPersenter(Object persenter) {
    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(JiuZhengPesonalActivity.this, mErrorMsg);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNumber = 1;
        mPresenter.getAddjiuzhenPerson(mToken, pageNumber, 10, result.toString());
        mRefreshLayout.finishRefresh(500);
    }
}
